package com.tv.enoguia.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.tv.enoguia.bean.Bussiness;
import com.tv.enoguia.bean.Celler;

public class MySQLiteHelper extends SQLiteOpenHelper{
	
	private static final String TAG = MySQLiteHelper.class.getSimpleName();
//	public static final String DATABASE_NAME = Environment.getExternalStorageDirectory()+"/vChat.db";
	public static final String DATABASE_NAME = "Enoguia.db";
	public static final int DATABASE_VERSION = 18;

	
	// tao cac truong
	public static final String TABLE_CELLER = "celler";
	public static final String TABLE_CELLER_LIKE = "celler_like";
	
	public static final String ID = "_id";
	public static final String KEY_ID_CELLER = "id_celler"; // id_cellar
	public static final String KEY_NAME_CATEGORY = "donom"; // name
	public static final String KEY_TOWN_NAME = "pob"; // townname
	public static final String KEY_CELLAR_NAME = "nom"; // cellar_name
	public static final String KEY_IMAGE_CELLAR = "foto"; // cellar_image
	
	public static final String KEY_HECT = "hect"; // hecta
	public static final String KEY_AMPOLLER = "ampolles"; // ampollers
	public static final String KEY_LOGO = "logo"; // logo
	public static final String KEY_ADDRESS = "address"; // address
	public static final String KEY_PHONE = "telefon"; // phone
	public static final String KEY_WEB = "web"; // web
	public static final String KEY_EMAIL = "email"; // email
	
	public static final String KEY_DES = "desc_en"; // desc_english
	public static final String KEY_DES_CA = "desc_ca"; // desc_english
	public static final String KEY_DES_ES = "desc_es"; // desc_english
	
	public static final String KEY_HORARI = "horari_en"; // horari
	public static final String KEY_HORARI_CA = "horari_ca"; // horari
	public static final String KEY_HORARI_ES = "horari_es"; // horari
	
	public static final String KEY_PRICES = "preu_en"; // price
	public static final String KEY_PRICES_CA = "preu_ca"; // price
	public static final String KEY_PRICES_ES = "preu_es"; // price
	
	public static final String KEY_LANGUAGE = "idiomes"; // language
	public static final String KEY_CAPITY_MIN = "capacitat_min"; // capity_min
	public static final String KEY_CAPITY_MAX = "capacitat_max"; // capity_max
	
	public static final String KEY_VIN = "vins_cata_en"; // vin
	public static final String KEY_VIN_CA = "vins_cata_ca"; // vin
	public static final String KEY_VIN_ES = "vins_cata_es"; // vin
	
	public static final String KEY_VISIT = "visita_vinyes"; // horari
	public static final String KEY_LON = "lon"; // horari
	public static final String KEY_LAT = "lat"; // horari
	public static final String KEY_LIKED = "liked"; // horari
	public static final String KEY_LISTA = "lista_vins"; // horari
	
	// tao bang message
	String CREATE_TABLE_CELLER = "CREATE TABLE IF NOT EXISTS " + TABLE_CELLER + "("+ ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_ID_CELLER+ " TEXT, " +KEY_NAME_CATEGORY+ " TEXT, " + KEY_TOWN_NAME + " TEXT, "
             + KEY_CELLAR_NAME + " TEXT, " + KEY_IMAGE_CELLAR + " TEXT, " + KEY_HECT + " TEXT, "+ KEY_AMPOLLER+ " TEXT, "+KEY_LOGO+ " TEXT, "+KEY_ADDRESS+ " TEXT, "+KEY_PHONE+ " TEXT, "
             + KEY_WEB + " TEXT, "+ KEY_EMAIL + " TEXT, "+ KEY_DES + " TEXT, "+ KEY_DES_CA + " TEXT, "+ KEY_DES_ES + " TEXT, "+ KEY_HORARI + " TEXT, "+ KEY_HORARI_CA + " TEXT, "+ KEY_HORARI_ES + " TEXT, "+ KEY_PRICES + " TEXT, "+ KEY_PRICES_CA + " TEXT, "+ KEY_PRICES_ES + " TEXT, "+ KEY_LANGUAGE + " TEXT, "
             + KEY_CAPITY_MIN + " TEXT, "+ KEY_CAPITY_MAX + " TEXT, "+ KEY_VIN + " TEXT, "+ KEY_VIN_CA + " TEXT, "+ KEY_VIN_ES + " TEXT, "+ KEY_VISIT + " TEXT, "+ KEY_LON + " TEXT, "+ KEY_LAT + " TEXT, "+KEY_LIKED + " INTEGER, "+KEY_LISTA + " TEXT)";
	
	String CREATE_TABLE_CELLER_LIKE = "CREATE TABLE IF NOT EXISTS " + TABLE_CELLER_LIKE + "("+ ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_ID_CELLER+ " TEXT, " +KEY_NAME_CATEGORY+ " TEXT, " + KEY_TOWN_NAME + " TEXT, "
            + KEY_CELLAR_NAME + " TEXT, " + KEY_IMAGE_CELLAR + " TEXT, " + KEY_HECT + " TEXT, "+ KEY_AMPOLLER+ " TEXT, "+KEY_LOGO+ " TEXT, "+KEY_ADDRESS+ " TEXT, "+KEY_PHONE+ " TEXT, "
            + KEY_WEB + " TEXT, "+ KEY_EMAIL + " TEXT, "+ KEY_DES + " TEXT, "+ KEY_DES_CA + " TEXT, "+ KEY_DES_ES + " TEXT, "+ KEY_HORARI + " TEXT, "+ KEY_HORARI_CA + " TEXT, "+ KEY_HORARI_ES + " TEXT, "+ KEY_PRICES + " TEXT, "+ KEY_PRICES_CA + " TEXT, "+ KEY_PRICES_ES + " TEXT, "+ KEY_LANGUAGE + " TEXT, "
            + KEY_CAPITY_MIN + " TEXT, "+ KEY_CAPITY_MAX + " TEXT, "+ KEY_VIN + " TEXT, "+ KEY_VIN_CA + " TEXT, "+ KEY_VIN_ES + " TEXT, "+ KEY_VISIT + " TEXT, "+ KEY_LON + " TEXT, "+ KEY_LAT + " TEXT, "+KEY_LIKED + " INTEGER, "+KEY_LISTA + " TEXT)";
	
	
	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_CELLER);
		db.execSQL(CREATE_TABLE_CELLER_LIKE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CELLER);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CELLER_LIKE);
		onCreate(db);
	}
	
	public void insertCeller(Celler celler){
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		
		values.put(KEY_ID_CELLER, celler.getId());
		values.put(KEY_NAME_CATEGORY, celler.getNameCategory());
		values.put(KEY_TOWN_NAME, celler.getTowName());
		values.put(KEY_CELLAR_NAME, celler.getCellarName());
		values.put(KEY_IMAGE_CELLAR, celler.getPhoto());
		values.put(KEY_HECT, celler.getHect());
		values.put(KEY_AMPOLLER, celler.getAmpoller());
		values.put(KEY_LOGO, celler.getLogo());
		values.put(KEY_ADDRESS, celler.getAddress());
		values.put(KEY_PHONE, celler.getPhone());
		values.put(KEY_WEB, celler.getWeb());
		values.put(KEY_EMAIL, celler.getEmail());
		
		values.put(KEY_DES, celler.getDesCriptionE());
		values.put(KEY_DES_CA, celler.getDesCriptionCA());
		values.put(KEY_DES_ES, celler.getDesCriptionES());
		
		values.put(KEY_HORARI, celler.getHorariE());
		values.put(KEY_HORARI_CA, celler.getHorariCA());
		values.put(KEY_HORARI_ES, celler.getHorariES());
		
		values.put(KEY_PRICES, celler.getPriceE());
		values.put(KEY_PRICES_CA, celler.getPriceCA());
		values.put(KEY_PRICES_ES, celler.getPriceES());
		
		values.put(KEY_LANGUAGE, celler.getLanguageE());
		values.put(KEY_CAPITY_MIN, celler.getCapityMin());
		values.put(KEY_CAPITY_MAX, celler.getCapityMax());
		
		values.put(KEY_VIN, celler.getVinE());
		values.put(KEY_VIN_CA, celler.getVinCA());
		values.put(KEY_VIN_ES, celler.getVinES());
		
		values.put(KEY_VISIT, celler.getVisitYes());
		values.put(KEY_LON, celler.getLon());
		values.put(KEY_LAT, celler.getLat());
		values.put(KEY_LIKED, 0);
		values.put(KEY_LISTA, celler.getListtaVins());
		
		int count = db.update(TABLE_CELLER, values, KEY_ID_CELLER + " = '"+celler.getId()+"'", null);
		
		
		
		if (count == 0){
			db.insert(TABLE_CELLER, null, values);
		}
		
//		db.insert(TABLE_CELLER, null, values);
		
		
		db.close();
	}
	
	public void deteleAllRecord(){
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_CELLER, "1=1", null);
		db.close();
	}
	
	public ArrayList<Celler> getAllCelers(){
		ArrayList<Celler> cellers = new ArrayList<Celler>();
		String selectQuery = "SELECT * FROM " + TABLE_CELLER ;
//		String selectQuery = "SELECT * FROM " + TABLE_MESSAGE ;
		
		Log.i("SQL", ""+selectQuery);
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);
		// duyet tat ca ca hang de add vao list
				if (c.moveToFirst()) {
					do {
						Celler celler = new Celler();
						
						celler.setId(c.getString(c.getColumnIndex(KEY_ID_CELLER)));
						celler.setNameCategory(c.getString(c.getColumnIndex(KEY_NAME_CATEGORY)));
						celler.setTowName(c.getString(c.getColumnIndex(KEY_TOWN_NAME)));
						celler.setCellarName(c.getString(c.getColumnIndex(KEY_CELLAR_NAME)));
						celler.setPhoto(c.getString(c.getColumnIndex(KEY_IMAGE_CELLAR)));
						celler.setHect(c.getString(c.getColumnIndex(KEY_HECT)));
						celler.setAmpoller(c.getString(c.getColumnIndex(KEY_AMPOLLER)));
						celler.setLogo(c.getString(c.getColumnIndex(KEY_LOGO)));
						celler.setAddress(c.getString(c.getColumnIndex(KEY_ADDRESS)));
						celler.setPhone(c.getString(c.getColumnIndex(KEY_PHONE)));
						celler.setWeb(c.getString(c.getColumnIndex(KEY_WEB)));
						celler.setEmail(c.getString(c.getColumnIndex(KEY_EMAIL)));
						
						celler.setDesCriptionE(c.getString(c.getColumnIndex(KEY_DES)));
						celler.setDesCriptionCA(c.getString(c.getColumnIndex(KEY_DES_CA)));
						celler.setDesCriptionES(c.getString(c.getColumnIndex(KEY_DES_ES)));
						
						celler.setHorariE(c.getString(c.getColumnIndex(KEY_HORARI)));
						celler.setHorariCA(c.getString(c.getColumnIndex(KEY_HORARI_CA)));
						celler.setHorariES(c.getString(c.getColumnIndex(KEY_HORARI_ES)));
						
						celler.setPriceE(c.getString(c.getColumnIndex(KEY_PRICES)));
						celler.setPriceCA(c.getString(c.getColumnIndex(KEY_PRICES_CA)));
						celler.setPriceES(c.getString(c.getColumnIndex(KEY_PRICES_ES)));
						
						celler.setLanguageE(c.getString(c.getColumnIndex(KEY_LANGUAGE)));
						celler.setCapityMin(c.getString(c.getColumnIndex(KEY_CAPITY_MIN)));
						celler.setCapityMax(c.getString(c.getColumnIndex(KEY_CAPITY_MAX)));
						
						celler.setVinE(c.getString(c.getColumnIndex(KEY_VIN)));
						celler.setVinCA(c.getString(c.getColumnIndex(KEY_VIN_CA)));
						celler.setVinES(c.getString(c.getColumnIndex(KEY_VIN_ES)));
						
						celler.setVisitYes(c.getString(c.getColumnIndex(KEY_VISIT)));
						celler.setLon(c.getString(c.getColumnIndex(KEY_LON)));
						celler.setLat(c.getString(c.getColumnIndex(KEY_LAT)));
						celler.setLiked(0);
						celler.setListtaVins(c.getString(c.getColumnIndex(KEY_LISTA)));
						
						// add list
						cellers.add(celler);
						
					} while (c.moveToNext());
				}
				c.close();
				if (db != null){
					db.close();	
				}
		
		return cellers;
	}
	
	
	public ArrayList<String> getCategories(){
		ArrayList<String> listCategories = new ArrayList<String>();
		SQLiteDatabase db = this.getReadableDatabase();
		String query = "SELECT DISTINCT "+KEY_NAME_CATEGORY+ " FROM "+ TABLE_CELLER + " WHERE "+KEY_NAME_CATEGORY+ " <> "+"'No adscrita'"+ " ORDER BY "+KEY_NAME_CATEGORY+" ASC";
		Log.i("DATA", "SQL: "+query);
		
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()){
			do {
				listCategories.add(cursor.getString(cursor.getColumnIndex(KEY_NAME_CATEGORY)));
			} while (cursor.moveToNext());
			
		}
		cursor.close();
		db.close();
		
		return listCategories;
	}
	
	public ArrayList<Bussiness> getBussinessCellar(String nameCategories){
		ArrayList<Bussiness> bussinesses = new ArrayList<Bussiness>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT DISTINCT "+KEY_ID_CELLER+","+KEY_TOWN_NAME+","+KEY_CELLAR_NAME+","+KEY_IMAGE_CELLAR+ " FROM "+ TABLE_CELLER + " WHERE "+KEY_NAME_CATEGORY+ " = '"+nameCategories+"'";
		Log.i("DATA", "SQL: "+query);
		
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()){
			do {
				Bussiness bussiness = new Bussiness();
				
				bussiness.setId(cursor.getString(cursor.getColumnIndex(KEY_ID_CELLER)));
				bussiness.setTownName(cursor.getString(cursor.getColumnIndex(KEY_TOWN_NAME)));
				bussiness.setCellarName(cursor.getString(cursor.getColumnIndex(KEY_CELLAR_NAME)));
				bussiness.setUrlBussiness("http://enoguia.cat/img/fotos/"+cursor.getString(cursor.getColumnIndex(KEY_IMAGE_CELLAR)));
				
				bussinesses.add(bussiness);
			} while (cursor.moveToNext());
			
		}
		cursor.close();
		db.close();
		
		return bussinesses;
	}
	
	
	// get detail cellar
	public Celler getDetailCeller(String id){
		
		SQLiteDatabase db = this.getReadableDatabase();
		Celler celler = new Celler();
		String query = "SELECT * FROM "+ TABLE_CELLER + " WHERE "+KEY_ID_CELLER+ " = '"+id+"'";
		Log.i("DATA", "SQL: "+query);
		
		Cursor c = db.rawQuery(query, null);
		if (c.moveToFirst()){
			do {
				
				celler.setId(id);
				celler.setNameCategory(c.getString(c.getColumnIndex(KEY_NAME_CATEGORY)));
				celler.setTowName(c.getString(c.getColumnIndex(KEY_TOWN_NAME)));
				celler.setCellarName(c.getString(c.getColumnIndex(KEY_CELLAR_NAME)));
				celler.setPhoto(c.getString(c.getColumnIndex(KEY_IMAGE_CELLAR)));
				celler.setHect(c.getString(c.getColumnIndex(KEY_HECT)));
				celler.setAmpoller(c.getString(c.getColumnIndex(KEY_AMPOLLER)));
				celler.setLogo(c.getString(c.getColumnIndex(KEY_LOGO)));
				celler.setAddress(c.getString(c.getColumnIndex(KEY_ADDRESS)));
				celler.setPhone(c.getString(c.getColumnIndex(KEY_PHONE)));
				celler.setWeb(c.getString(c.getColumnIndex(KEY_WEB)));
				celler.setEmail(c.getString(c.getColumnIndex(KEY_EMAIL)));
				
				celler.setDesCriptionE(c.getString(c.getColumnIndex(KEY_DES)));
				celler.setDesCriptionCA(c.getString(c.getColumnIndex(KEY_DES_CA)));
				celler.setDesCriptionES(c.getString(c.getColumnIndex(KEY_DES_ES)));
				
				celler.setHorariE(c.getString(c.getColumnIndex(KEY_HORARI)));
				celler.setHorariCA(c.getString(c.getColumnIndex(KEY_HORARI_CA)));
				celler.setHorariES(c.getString(c.getColumnIndex(KEY_HORARI_ES)));
				
				celler.setPriceE(c.getString(c.getColumnIndex(KEY_PRICES)));
				celler.setPriceCA(c.getString(c.getColumnIndex(KEY_PRICES_CA)));
				celler.setPriceES(c.getString(c.getColumnIndex(KEY_PRICES_ES)));
				
				celler.setLanguageE(c.getString(c.getColumnIndex(KEY_LANGUAGE)));
				celler.setCapityMin(c.getString(c.getColumnIndex(KEY_CAPITY_MIN)));
				celler.setCapityMax(c.getString(c.getColumnIndex(KEY_CAPITY_MAX)));
				
				celler.setVinE(c.getString(c.getColumnIndex(KEY_VIN)));
				celler.setVinCA(c.getString(c.getColumnIndex(KEY_VIN_CA)));
				celler.setVinES(c.getString(c.getColumnIndex(KEY_VIN_ES)));
				
				celler.setVisitYes(c.getString(c.getColumnIndex(KEY_VISIT)));
				celler.setLon(c.getString(c.getColumnIndex(KEY_LON)));
				celler.setLat(c.getString(c.getColumnIndex(KEY_LAT)));
//				celler.setLiked(c.getInt(c.getColumnIndex(KEY_LIKED)));
				celler.setListtaVins(c.getString(c.getColumnIndex(KEY_LISTA)));
				
				
			} while (c.moveToNext());
			
		}
		c.close();
		db.close();
		
		return celler;
	}
	
	
	
	
	public ArrayList<Celler> getAllCelersMap(){
		ArrayList<Celler> cellers = new ArrayList<Celler>();
		String selectQuery = "SELECT DISTINCT "+KEY_ID_CELLER+","+KEY_TOWN_NAME+","+KEY_CELLAR_NAME+","+KEY_IMAGE_CELLAR+","+KEY_LAT+","+KEY_LON+ " FROM "+ TABLE_CELLER ;
//		String selectQuery = "SELECT * FROM " + TABLE_MESSAGE ;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);
		// duyet tat ca ca hang de add vao list
				if (c.moveToFirst()) {
					do {
						Celler celler = new Celler();
						
						celler.setId(c.getString(c.getColumnIndex(KEY_ID_CELLER)));
						celler.setTowName(c.getString(c.getColumnIndex(KEY_TOWN_NAME)));
						celler.setCellarName(c.getString(c.getColumnIndex(KEY_CELLAR_NAME)));
						celler.setPhoto(c.getString(c.getColumnIndex(KEY_IMAGE_CELLAR)));
						celler.setLon(c.getString(c.getColumnIndex(KEY_LON)));
						celler.setLat(c.getString(c.getColumnIndex(KEY_LAT)));
						// add list
						cellers.add(celler);
						
					} while (c.moveToNext());
				}
				c.close();
				if (db != null){
					db.close();	
				}
		
		return cellers;
	}
	
	
	
	// danh cho thang like
	public void insertLike(Celler celler, int liked){
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		
		values.put(KEY_LIKED, liked);
		db.update(TABLE_CELLER, values, KEY_ID_CELLER + " = ?", new String[]{celler.getId()});
		db.close();
	}
	
	public void insertIntoTableLike(Celler celler, int liked){
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		
		values.put(KEY_ID_CELLER, celler.getId());
		values.put(KEY_NAME_CATEGORY, celler.getNameCategory());
		values.put(KEY_TOWN_NAME, celler.getTowName());
		values.put(KEY_CELLAR_NAME, celler.getCellarName());
		values.put(KEY_IMAGE_CELLAR, celler.getPhoto());
		values.put(KEY_HECT, celler.getHect());
		values.put(KEY_AMPOLLER, celler.getAmpoller());
		values.put(KEY_LOGO, celler.getLogo());
		values.put(KEY_ADDRESS, celler.getAddress());
		values.put(KEY_PHONE, celler.getPhone());
		values.put(KEY_WEB, celler.getWeb());
		values.put(KEY_EMAIL, celler.getEmail());
		
		values.put(KEY_DES, celler.getDesCriptionE());
		values.put(KEY_DES_CA, celler.getDesCriptionCA());
		values.put(KEY_DES_ES, celler.getDesCriptionES());
		
		values.put(KEY_HORARI, celler.getHorariE());
		values.put(KEY_HORARI_CA, celler.getHorariCA());
		values.put(KEY_HORARI_ES, celler.getHorariES());
		
		values.put(KEY_PRICES, celler.getPriceE());
		values.put(KEY_PRICES_CA, celler.getPriceCA());
		values.put(KEY_PRICES_ES, celler.getPriceES());
		
		values.put(KEY_LANGUAGE, celler.getLanguageE());
		values.put(KEY_CAPITY_MIN, celler.getCapityMin());
		values.put(KEY_CAPITY_MAX, celler.getCapityMax());
		
		values.put(KEY_VIN, celler.getVinE());
		values.put(KEY_VIN_CA, celler.getVinCA());
		values.put(KEY_VIN_ES, celler.getVinES());
		
		values.put(KEY_VISIT, celler.getVisitYes());
		values.put(KEY_LON, celler.getLon());
		values.put(KEY_LAT, celler.getLat());
		values.put(KEY_LIKED, liked);
		values.put(KEY_LISTA, celler.getListtaVins());
		
		
		db.insert(TABLE_CELLER_LIKE, null, values);
		
		
		db.close();
		
	}
	
	public int getLiked(String id){
		int liked = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		
		return liked;
		
	}
	
	public void deteleBussiness(){
		
	}
	
	public ArrayList<Bussiness> getBussinessLike(){
		ArrayList<Bussiness> bussinesses = new ArrayList<Bussiness>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT DISTINCT "+KEY_ID_CELLER+","+KEY_TOWN_NAME+","+KEY_CELLAR_NAME+","+KEY_IMAGE_CELLAR+ " FROM "+ TABLE_CELLER_LIKE + " WHERE "+KEY_LIKED+ " = 1" ;
		Log.i("DATA", "SQL: "+query);
		
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()){
			do {
				Bussiness bussiness = new Bussiness();
				
				bussiness.setId(cursor.getString(cursor.getColumnIndex(KEY_ID_CELLER)));
				bussiness.setTownName(cursor.getString(cursor.getColumnIndex(KEY_TOWN_NAME)));
				bussiness.setCellarName(cursor.getString(cursor.getColumnIndex(KEY_CELLAR_NAME)));
				bussiness.setUrlBussiness("http://enoguia.cat/img/fotos/"+cursor.getString(cursor.getColumnIndex(KEY_IMAGE_CELLAR)));
				
				bussinesses.add(bussiness);
			} while (cursor.moveToNext());
			
		}
		cursor.close();
		db.close();
		
		return bussinesses;
	}
	
	
public Celler getDetailCellerLike(String id){
		
		SQLiteDatabase db = this.getReadableDatabase();
		Celler celler = new Celler();
		String query = "SELECT * FROM "+ TABLE_CELLER_LIKE + " WHERE "+KEY_ID_CELLER+ " = '"+id+"'";
		Log.i("DATA", "SQL: "+query);
		
		Cursor c = db.rawQuery(query, null);
		if (c.moveToFirst()){
			do {
				
				celler.setId(id);
				celler.setNameCategory(c.getString(c.getColumnIndex(KEY_NAME_CATEGORY)));
				celler.setTowName(c.getString(c.getColumnIndex(KEY_TOWN_NAME)));
				celler.setCellarName(c.getString(c.getColumnIndex(KEY_CELLAR_NAME)));
				celler.setPhoto(c.getString(c.getColumnIndex(KEY_IMAGE_CELLAR)));
				celler.setHect(c.getString(c.getColumnIndex(KEY_HECT)));
				celler.setAmpoller(c.getString(c.getColumnIndex(KEY_AMPOLLER)));
				celler.setLogo(c.getString(c.getColumnIndex(KEY_LOGO)));
				celler.setAddress(c.getString(c.getColumnIndex(KEY_ADDRESS)));
				celler.setPhone(c.getString(c.getColumnIndex(KEY_PHONE)));
				celler.setWeb(c.getString(c.getColumnIndex(KEY_WEB)));
				celler.setEmail(c.getString(c.getColumnIndex(KEY_EMAIL)));
				
				celler.setDesCriptionE(c.getString(c.getColumnIndex(KEY_DES)));
				celler.setDesCriptionCA(c.getString(c.getColumnIndex(KEY_DES_CA)));
				celler.setDesCriptionES(c.getString(c.getColumnIndex(KEY_DES_ES)));
				
				celler.setHorariE(c.getString(c.getColumnIndex(KEY_HORARI)));
				celler.setHorariCA(c.getString(c.getColumnIndex(KEY_HORARI_CA)));
				celler.setHorariES(c.getString(c.getColumnIndex(KEY_HORARI_ES)));
				
				celler.setPriceE(c.getString(c.getColumnIndex(KEY_PRICES)));
				celler.setPriceCA(c.getString(c.getColumnIndex(KEY_PRICES_CA)));
				celler.setPriceES(c.getString(c.getColumnIndex(KEY_PRICES_ES)));
				
				celler.setLanguageE(c.getString(c.getColumnIndex(KEY_LANGUAGE)));
				celler.setCapityMin(c.getString(c.getColumnIndex(KEY_CAPITY_MIN)));
				celler.setCapityMax(c.getString(c.getColumnIndex(KEY_CAPITY_MAX)));
				
				celler.setVinE(c.getString(c.getColumnIndex(KEY_VIN)));
				celler.setVinCA(c.getString(c.getColumnIndex(KEY_VIN_CA)));
				celler.setVinES(c.getString(c.getColumnIndex(KEY_VIN_ES)));
				
				celler.setVisitYes(c.getString(c.getColumnIndex(KEY_VISIT)));
				celler.setLon(c.getString(c.getColumnIndex(KEY_LON)));
				celler.setLat(c.getString(c.getColumnIndex(KEY_LAT)));
				celler.setLiked(c.getInt(c.getColumnIndex(KEY_LIKED)));
				celler.setListtaVins(c.getString(c.getColumnIndex(KEY_LISTA)));
				
				
			} while (c.moveToNext());
			
		}
		c.close();
		db.close();
		
		return celler;
	}

	public ArrayList<String> getIdLike(){
		ArrayList<String> ids = new ArrayList<String>();
		SQLiteDatabase db = this.getReadableDatabase();
		String query = "SELECT DISTINCT "+KEY_ID_CELLER+ " FROM "+ TABLE_CELLER_LIKE;
		Log.i("DATA", "SQL: "+query);
		
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()){
			do {
				ids.add(cursor.getString(cursor.getColumnIndex(KEY_ID_CELLER)));
			} while (cursor.moveToNext());
			
		}
		cursor.close();
		db.close();
		
		return ids;
	}
	
	public void unlike(String id){
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_CELLER_LIKE, ""+KEY_ID_CELLER+" = '"+id+"'", null);
		db.close();
	}
	
	
	
}
